package com.xzy.es.demo.index;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
import org.elasticsearch.cluster.metadata.AliasMetadata;
import org.elasticsearch.cluster.metadata.MappingMetadata;
import org.elasticsearch.common.settings.Settings;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 索引相关操作
 *
 * @author xzy
 * @date 2021/10/2016:38
 */
@Slf4j
public class IndexMain {

    /**
     * 创建索引
     *
     * @param indexName - 索引名称
     * @throws IOException -
     */
    public static void insertIndex(RestHighLevelClient esClient, String indexName) throws IOException {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
        CreateIndexResponse createIndexResponse = esClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        if (createIndexResponse.isAcknowledged()) {
            log.info("\n索引创建成功：{}\n", indexName);
        } else {
            log.warn("\n索引创建失败：{}\n", indexName);
        }
    }

    /**
     * 查询索引
     *
     * @param indexName - 索引名称
     */
    public static void selectIndex(RestHighLevelClient esClient, String indexName) throws IOException {
        GetIndexRequest getIndexRequest = new GetIndexRequest(indexName);
        GetIndexResponse getIndexResponse = esClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);

        Map<String, List<AliasMetadata>> aliases = getIndexResponse.getAliases();
        Map<String, MappingMetadata> mappings = getIndexResponse.getMappings();
        Map<String, Settings> settings = getIndexResponse.getSettings();

        log.info("\naliases：{}\nmappings：{}\nsettings:{}\n",
                JSON.toJSONString(aliases, SerializerFeature.IgnoreErrorGetter),
                JSON.toJSONString(mappings, SerializerFeature.IgnoreErrorGetter),
                JSON.toJSONString(settings, SerializerFeature.IgnoreErrorGetter)
        );
    }

    /**
     * 删除索引
     *
     * @param indexName - 索引名称
     */
    public static void deleteIndex(RestHighLevelClient esClient, String indexName) throws IOException {
        DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(indexName);
        AcknowledgedResponse deleteAck = esClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);
        if (deleteAck.isAcknowledged()) {
            log.info("\n索引删除成功：{}\n", indexName);
        } else {
            log.warn("\n索引删除失败：{}\n", indexName);
        }
    }
}
